#import "AppDelegate+PushService.h"
#import "CallUserManger.h"
@implementation AppDelegate (PushService)
-(void)intPushConfigWidthLaunchOptions:(NSDictionary *)launchOptions
{
    [UMConfigure setLogEnabled:YES];
    [UMConfigure initWithAppkey:UMengKey channel:@"App Store"];
    UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert|UMessageAuthorizationOptionSound;
    if (([[[UIDevice currentDevice] systemVersion]intValue]>=8)&&([[[UIDevice currentDevice] systemVersion]intValue]<10)) {
        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.identifier = @"action1_identifier";
        action1.title=ASLocalizedString(@"打开应用");
        action1.activationMode = UIUserNotificationActivationModeForeground;
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  
        action2.identifier = @"action2_identifier";
        action2.title=ASLocalizedString(@"忽略");
        action2.activationMode = UIUserNotificationActivationModeBackground;
        action2.authenticationRequired = YES;
        action2.destructive = YES;
        UIMutableUserNotificationCategory *actionCategory1 =[[UIMutableUserNotificationCategory alloc] init];
        actionCategory1.identifier = @"category1";
        [actionCategory1 setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
        NSSet *categories = [NSSet setWithObjects:actionCategory1, nil];
        entity.categories=categories;
    }
    if ([[[UIDevice currentDevice] systemVersion]intValue]>=10) {
        UNNotificationAction *action1_ios10 = [UNNotificationAction actionWithIdentifier:@"action1_identifier" title:ASLocalizedString(@"打开应用")options:UNNotificationActionOptionForeground];
        UNNotificationAction *action2_ios10 = [UNNotificationAction actionWithIdentifier:@"action2_identifier" title:ASLocalizedString(@"忽略")options:UNNotificationActionOptionForeground];
        UNNotificationCategory *category1_ios10 = [UNNotificationCategory categoryWithIdentifier:@"category1" actions:@[action1_ios10,action2_ios10]   intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
        NSSet *categories = [NSSet setWithObjects:category1_ios10, nil];
        entity.categories=categories;
    }
    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert;
    [UNUserNotificationCenter currentNotificationCenter].delegate=self;
    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
        }else
        {
        }
    }];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
-(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"dtoken %@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                  stringByReplacingOccurrencesOfString: @">" withString: @""]
                 stringByReplacingOccurrencesOfString: @" " withString: @""]);
    [UMessage registerDeviceToken:deviceToken];
    SaveObjToUserDefault(deviceToken, OmDeviceTokenKey);
    NSLog(@"%@",deviceToken);
    NSLog(@"%@",curUser.id);
    if(StrValid(curUser.id))
    {
        [UMessage setAlias:curUser.id  type:@"buguniao" response:^(id  _Nullable responseObject, NSError * _Nullable error) {
            NSLog(ASLocalizedString(@"绑定别名%@===%@"),responseObject,error);
        }];
        NSString *Furl = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"is_agency"];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [CYNET POST:Furl parameters:parameters responseCache:nil success:^(id responseObject) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"DaiLiQuanXian"];
        } failure:^(NSString *error) {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"DaiLiQuanXian"];
        } hasCache:NO];
    }
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@", error.localizedDescription);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"userinfo:%@",userInfo);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [UMessage setAutoAlert:NO];
    if([[[UIDevice currentDevice] systemVersion]intValue] < 10){
        [UMessage didReceiveRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [UMessage setAutoAlert:NO];
        [UMessage didReceiveRemoteNotification:userInfo];
    }else{
    }
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [UMessage didReceiveRemoteNotification:userInfo];
        if([userInfo valueForKey:@"custom"] != nil)
        {
            [self distributingNotificationMessages:userInfo];
        }
    }else{
    }
}
-(void)distributingNotificationMessages:(NSDictionary *)message
{
    NSString *customStr = [message valueForKey:@"custom"];
    if (customStr == nil) {
        return;
    }
    NSData *jsonData = [customStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *custom = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    NSString *action = [NSString stringWithFormat:@"%@",[custom valueForKey:@"action"]];
    if([action isEqualToString:@"12"])
    {
        [[CallUserManger alloc] CallRemoveMessage:custom];
    }
}
@end
